package com.lineage.server.model.weaponskill;

import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class W_SK0010 extends L1WeaponSkillType {
  private static final Log _log = LogFactory.getLog(W_SK0010.class);
  
  private static final Random _random = new Random();
  
  public static L1WeaponSkillType get() {
    return new W_SK0010();
  }
  
  public double start_weapon_skill(L1PcInstance pc, L1Character target, L1ItemInstance weapon, double srcdmg) {
    try {
      int chance = _random.nextInt(1000);
      if (random(weapon) + pc.getWeaponSkillChance() * 10 >= chance) {
        DmgSet dmgSet = new DmgSet(pc, target, weapon, srcdmg);
        dmgSet.begin();
        return 0.0D;
      } 
      return 0.0D;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return 0.0D;
    } 
  }
  
  private class DmgSet implements Runnable {
    private int _timeCounter;
    
    private double _srcdmg;
    
    private final L1PcInstance _pc;
    
    private final L1Character _cha;
    
    private final L1ItemInstance _weapon;
    
    private DmgSet(L1PcInstance pc, L1Character cha, L1ItemInstance weapon, double srcdmg) {
      this._timeCounter = 0;
      this._srcdmg = 0.0D;
      this._cha = cha;
      this._pc = pc;
      this._srcdmg = srcdmg;
      this._weapon = weapon;
    }
    
    public void run() {
      try {
        if (this._cha.hasSkillEffect(8050))
          return; 
        if (this._cha.hasSkillEffect(31)) {
          this._cha.removeSkillEffect(31);
          int castgfx2 = SkillsTable.get().getTemplate(31).getCastGfx2();
          this._cha.broadcastPacketAll((ServerBasePacket)new S_SkillSound(this._cha.getId(), castgfx2));
          if (this._cha instanceof L1PcInstance) {
            L1PcInstance tgpc = (L1PcInstance)this._cha;
            tgpc.sendPacketsAll((ServerBasePacket)new S_SkillSound(tgpc.getId(), castgfx2));
          } 
          return;
        } 
        if (this._cha.hasSkillEffect(153))
          this._cha.removeSkillEffect(153); 
        W_SK0010.this.show(this._pc, this._cha);
        while (this._timeCounter < W_SK0010.this._type2 && 
          this._cha != null) {
          if (this._cha.isDead())
            break; 
          attack();
          this._timeCounter++;
          Thread.sleep(1000L);
        } 
      } catch (Throwable e) {
        W_SK0010._log.error(e.getLocalizedMessage(), e);
      } 
    }
    
    private void begin() {
      GeneralThreadPool.get().schedule(this, 100L);
    }
    
    private void attack() {
      this._cha.setSkillEffect(8050, W_SK0010.this._type2 * 1000);
      double damage = DmgAcMr.getDamage(this._pc);
      if (this._cha.getCurrentHp() - damage <= 0.0D && this._cha.getCurrentHp() != 1) {
        damage = (this._cha.getCurrentHp() - 1);
      } else if (this._cha.getCurrentHp() == 1) {
        damage = 0.0D;
      } 
      double outdmg = W_SK0010.this.dmg1() + W_SK0010.this.dmg2(this._srcdmg) + W_SK0010.this.dmg3(this._pc) + 
        damage;
      outdmg = W_SK0010.this.calc_dmg(this._pc, this._cha, outdmg, this._weapon);
      if (W_SK0010.this._type3 > 0)
        outdmg *= W_SK0010.this._type3 / 100.0D; 
      if (this._pc.getWeaponSkillDmg() != 1.0D)
        outdmg += outdmg * this._pc.getWeaponSkillDmg(); 
      if (this._cha instanceof L1PcInstance) {
        L1PcInstance pc = (L1PcInstance)this._cha;
        pc.receiveDamage((L1Character)this._pc, outdmg, true, true);
      } else if (this._cha instanceof L1NpcInstance) {
        L1NpcInstance npc = (L1NpcInstance)this._cha;
        npc.receiveDamage((L1Character)this._pc, (int)outdmg);
      } 
    }
  }
}
